<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        class="filter-item"
        type="primary"
        size="small"
        icon="el-icon-circle-plus"
        @click="$router.push(add_url)"
        >添加</el-button
      >
    </div>
    <el-table
      :data="tableData"
      v-loading="loadList"
      style="width: 100%;"
      element-loading-text="拼命加载中..."
      border
    >
      <el-table-column prop="carrierName" label="运营商名称"></el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
      <el-table-column prop="carrierPicture" label="运营商主图" width="200px">
        <template slot-scope="scope">
          <img :src="scope.row.carrierPicture" />
        </template>
      </el-table-column>
      <el-table-column prop="detailImage" label="资费详情图" width="200px">
        <template slot-scope="scope">
          <img :src="scope.row.detailImage" />
        </template>
      </el-table-column>
      <el-table-column prop="qrcode" label="二维码图片" width="200px">
        <template slot-scope="scope">
          <img :src="scope.row.qrcode" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="edit(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger" @click="del(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!loadList" class="pagination-container"></div>
  </div>
</template>

<script>
import { getList, del } from "./api/index";
export default {
  data() {
    return {
      loadList: true,
      tableData: [],
      add_url: "/aquare/campus-card/classify/add",
      edit_url: (id) => {
        return `/aquare/campus-card/classify/${id}/edit`;
      },
    };
  },
  created() {
    this.refresh_data();
  },
  methods: {
    refresh_data() {
      getList()
        .then((data) => {
          console.log(data, "=====");
          if (data.code === 1) {
            this.tableData = data.data;
          } else {
            this.$message.error(data.description);
            this.currentPage = 1;
          }
          this.loadList = false;
        })
        .catch((err) => {
          console.log(err);
          this.loadList = false;
        });
    },
    edit(id) {
      this.$router.push(this.edit_url(id));
    },
    async del(item) {
      this.$confirm("确认删除该条数据？", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        this.loadList = true;
        const res = await del(item);
        if (res.code === 1) {
          this.refresh_data();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
img{
  width: 100%;
  height: auto;
}
</style>
